﻿using System.Linq;
using Sub.TaskPlanningDesk.Model.Objects;

using User = Sub.TaskPlanningDesk.Model.Objects.User;
using Sub.TaskPlanningDesk.Model.Infrastructure;

namespace Sub.TaskPlanningDesk.Model.Repositories
{
	public class UserRepository : IUserRepository
	{
		TaskPlanningDBEntities Context;

		public UserRepository()
		{
			Context = new TaskPlanningDBEntities("TaskPlanningDBEntities");
		}

		public User GetUserByID(int userID)
		{
			var userDB = Context.Users.SingleOrDefault(r => r.ID == userID);
			return new User()
			{
				ID = userDB.ID,
				Name = userDB.Name,
				Password = userDB.Password
			};
		}

		public User FindUserByName(string userName)
		{
			var userDB = Context.Users.SingleOrDefault(r => r.Name == userName);
			if (userDB == null)
				return null;

			return new User()
			{
				ID = userDB.ID,
				Name = userDB.Name,
				Password = userDB.Password
			};
		}
	}
}
